"""
    Created By Khun on 2020/4/7
"""

__author__ = 'Khun'

from flask import Flask

from app.models import User, Manage
from app.vipadmin import vipadmin
from app.models.base import db
from app.manage import manage

from flask_login import LoginManager

login_manager = LoginManager()


@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))


def create_app():

    app = Flask(__name__, static_folder='templates')

    # 加载配置文件
    app.config.from_object('app.settings')

    # 数据库插件
    db.init_app(app)
    with app.app_context():
        db.create_all()
    # 登录插件
    login_manager.init_app(app)
    login_manager.login_view = 'vipadmin.login'
    login_manager.login_message = '请先登录'
    login_manager.login_message_category = 'danger'

    # 注册蓝图
    app.register_blueprint(vipadmin)
    app.register_blueprint(manage)

    return app
